﻿using IOP.ISEA;
using IOP.SgrA;
using IOP.SgrA.SilkNet.Vulkan;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using System;
using System.IO;
using System.Runtime.InteropServices;
using System.Text.Json;
using System.Threading.Tasks;

namespace VkSample69
{
    class Program
    {
        static async Task Main(string[] args)
        {
            CreateISEA(true);
            var h = Host.CreateDefaultBuilder(args)
                .ConfigureAppConfiguration((config) =>
                {
                    config.SetBasePath(Directory.GetCurrentDirectory());
                    if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) config.AddVulkanJsonFile("Vulkan.json");
                    else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) config.AddVulkanJsonFile("Vulkan-Linux.json");
                    else throw new NotSupportedException("Not supported Platform");
                })
                .ConfigureSgrAHost((sgra) =>
                {
                    sgra.AddVulkanEnvironmental();
                    sgra.UseStartup<Startup>();
                }).ConfigureLogging((logger) => logger.AddConsole()); ;
            await h.RunConsoleAsync();
        }

        static void CreateISEA(bool isNeed)
        {
            var texPath = Path.Combine(AppContext.BaseDirectory, "Assets", "Textures");
            var imageJson = JsonDocument.Parse(File.ReadAllText(Path.Combine(texPath, "board.json")));
            var boardJson = imageJson.RootElement.GetProperty("Board").GetRawText();
            var samplerJson = imageJson.RootElement.GetProperty("Sampler").GetRawText();
            ISEADocument document = new ISEADocument();
            ConfigNode boardConfig = document.CreateConfigNode("BoardConfig", boardJson, ConfigEngine.Vulkan);
            ConfigNode sampler = document.CreateConfigNode("Sampler", samplerJson, ConfigEngine.Vulkan);
            IOP.ISEA.ImageNode image = document.CreateImageNode("BoardGreen", Path.Combine(texPath, "boardGreen.bn3dtex"));
            IOP.ISEA.ImageNode image2 = document.CreateImageNode("BoardRed", Path.Combine(texPath, "boardRed.bn3dtex"));
            BallData ballData = new BallData();
            ballData.GenBallData(10);
            MeshNode mesh = document.CreateEmptyMeshNode($"BallMesh", (int)ballData.VCount)
                .AddMeshData(ballData.Data, 12, 0, MeshDataPurpose.COMPLEX, true);
            TextureNode tex = document.CreateTextureNode($"TexGreen", TextureUsage.CombinedImageSampler, image,
                boardConfig, sampler, 1);
            TextureNode tex2 = document.CreateTextureNode($"TexRed", TextureUsage.CombinedImageSampler, image2,
                boardConfig, sampler, 1);
            document.CreateRenderObjectNode($"BallGreen", mesh, tex);
            document.CreateRenderObjectNode($"BallRed", mesh, tex2);
            document.Save(AppContext.BaseDirectory, "Test69");
        }
    }
}
